package com.Players;

import com.baseinfinityrun.Assets;

public class PresentPlayer extends Player
{
	public boolean canJump;
	public float jumpForce, jump, totalGravity;
	
	enum STATE
	{
		JUMPING,
		ONPLATFORM,
		ONGROUND,
		FALLING
	}

	public PresentPlayer(float x, float y, float w, float h) 
	{
		super(x, y, w, h);
		this.animation = Assets.player;
		playerState = STATE.JUMPING;
	}
	
	public void Update(float deltaTime)
	{
		if(playerState == STATE.JUMPING)
		{
			Jumping(deltaTime);
			Gravity(deltaTime);
		}
		
		if(playerState == STATE.ONGROUND || playerState == STATE.ONPLATFORM)
		{
			Jumping(deltaTime);
		}
		
		if(playerState == STATE.FALLING)
		{
			Gravity(deltaTime);
		}
		
		super.Update(deltaTime);
	}

	@Override
	public void TouchLeft()
	{
		canJump = true;
	}

	@Override
	public void TouchRight()
	{
		if(canJump)
		{
			canJump = false;
			if(playerState == STATE.ONPLATFORM)
			{
				this.position.add(0,0.7f);
				jumpForce = 3;
			}
			
			else
			{
				jumpForce = 7;
			}
			
			playerState = STATE.JUMPING;
		}
	}
	
	private void Jumping(float deltaTime)
	{
		if(jumpForce > 0)
		{
			jump = jumpForce * deltaTime;
			jumpForce -= jump * 1.2f;
			this.accel.add(0,jump);
		}
		
		else
		{
			jump = 0;
		}
	}

	void Gravity(float deltaTime)
	{
		totalGravity += 0.17f * deltaTime;
		
		this.accel.sub(0,totalGravity);
		
		if(this.rectCollider.lowerLeft.y  < 0.6f)
		{
			this.position.y = this.rectCollider.height * 0.5f + 0.6f;
			this.SetState(1);
		}
	}
	
	@Override
	public String GetState()
	{
		if(playerState == STATE.JUMPING)
		{
			return "jumping";
		}
		
		else if(playerState == STATE.ONGROUND)
		{
			return "onground";
		}
		
		else if(playerState == STATE.ONPLATFORM)
		{
			return "onplatform";
		}
		
		else if(playerState == STATE.FALLING)
		{
			return "onplatform";
		}	
		
		return "";
	}

	@Override
	public void SetState(int state)
	{
		switch(state)
		{
			case 0:
				playerState = STATE.JUMPING;
			break;
			
			case 1:
				playerState = STATE.ONGROUND;
				totalGravity = 0;
				jumpForce = 0;
				break;
		
			case 2:
				playerState = STATE.ONPLATFORM;
				totalGravity = 0;
				jumpForce = 0;
				break;
		
			case 3:
				playerState = STATE.FALLING;
				break;
				
			default:
				break;
		}
	}

}
